export default {
	namespaced: true,
	state() {
		return {
			popShow:false,
			popData:null, //存放弹窗数据包
			editIdx:-1, //区分新增、编辑
			cartList: [
			// 	{
			// 	french: "Napoléon aux fraises",
			// 	id: "10090",
			// 	img: "https://static.mcake.com/new_goods/napolunmeilian/N0201/list/1.jpg",
			// 	name: "拿破仑莓恋",
			// 	objectId: "6232a2f025bef044fcbb51a0",
			// 	price: "218.00",
			// 	num:1
			// },{
			// 	french: "Napoléon aux",
			// 	id: "10091",
			// 	img: "https://static.mcake.com/new_goods/napolunmeilian/N0201/list/1.jpg",
			// 	name: "破仑莓恋",
			// 	objectId: "6232a2f025bef044fvbb51a0",
			// 	price: "213.00",
			// 	num:2
			// },
			]
		}
	},
	getters:{
		allInfo(state){
			let allCheck = true
			let allPrice = 0
			let {cartList} = state
			for(let i=0;i<cartList.length;i++){
				if(!cartList[i].isCheck){
					allCheck = false
				}
				if(cartList[i].isCheck){
					let {num,dropIdx} = cartList[i]
					allPrice += cartList[i].list[dropIdx].price * num
				}
			}
			uni.setStorage({
				key:'uni-cart-list',
				data:state.cartList
			})
			return {
				allCheck,
				allPrice
			}
		}
	},
	mutations:{
		togglePopShow(state,bool){
			state.popShow = bool
		},
		popDataMut(state,cakeObj){
			state.popData = cakeObj
		},
		cartAddMut(state,cakeObj){ //新增、编辑
			let {cartList,editIdx} = state
			if(editIdx!=-1){   //编辑
				cartList[editIdx] = cakeObj
				return
			}
			for(let i=0;i<cartList.length;i++){
				let {cakeId,dropIdx} = cartList[i]
				if(cakeId==cakeObj.objectId&&dropIdx==cakeObj.dropIdx){
					state.cartList[i].num += cakeObj.num //修改已有商品数量
					return
				}
			}
			cakeObj.cakeId = cakeObj.objectId
			delete cakeObj.objectId
			state.cartList.push(cakeObj) //新增新商品
		},
		cartInitMut(state){ //初始化购物车数据
			uni.getStorage({
				key:'uni-cart-list',
				success: ({data}) => {
					console.log(data);
					state.cartList = data
				}
			})
		},
		cartCheckMut(state,idx){ //单选
			state.cartList[idx].isCheck = !state.cartList[idx].isCheck
		},
		cartAllCheckMut(state,bool){
			state.cartList.forEach(item=>{
				item.isCheck = !bool
			})
		},
		editIdxMut(state,idx){
			state.editIdx = idx
		},
		cartDelMut(state,idx){
			state.cartList.splice(idx,1)
		}
	}
}
